package com.fragment;

import java.util.Date;

import util.ApplicationConstant;
import interfaces.NavigateToLocateDetail;

import com.activity.GetDirection;
import com.activity.MainAppActivity;
import com.activity.MapActivity;
import com.activity.PopupImages;
import com.activity.R;

import dao.FileBookmarkRecent;
import dao.LoadLocate;
import entities.Locate;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.text.format.DateFormat;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class LocateDetailsFragment extends Fragment implements NavigateToLocateDetail{
	//title textview
	private TextView txtContact;
	private TextView txtTilelPhone;
	private TextView txtTitleWebsite;
	private TextView txtTitleEmail;
	private TextView txtTitleInfor;
	//-------------------
	public static ImageView imageView;
	private TextView txtAddress;
	private TextView txtName;
	private TextView txtWebsite;
	private TextView txtPhone;
	private TextView txtEmail;
	private TextView txtInfor;
	private TextView txtDistance;
	private ImageButton btnMarkBack;
	private ImageButton btnBookmark;
	private ImageButton btnCall;
	private ImageButton btnGetDirection;
	public static Locate locate;
	private ProgressDialog progressDialog;
	private String BOOKMARK_SUCCESS="";
	private String LOADING_IMG="";
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View root=inflater.inflate(R.layout.details_locate, container, false);
		progressDialog=new ProgressDialog(getActivity());
		LOADING_IMG=getResources().getString(R.string.Loading_images);
		progressDialog.setMessage(LOADING_IMG);
		//find view by id
		txtTilelPhone=(TextView) root.findViewById(R.id.txt_phone);
		txtTilelPhone.setTypeface(MainAppActivity.typeFaceFont);
		
		txtTitleWebsite=(TextView) root.findViewById(R.id.txt_website);
		txtTitleWebsite.setTypeface(MainAppActivity.typeFaceFont);
		
		txtTitleEmail=(TextView) root.findViewById(R.id.txt_email);
		txtTitleEmail.setTypeface(MainAppActivity.typeFaceFont);
		
		txtTitleInfor=(TextView) root.findViewById(R.id.txt_infor);
		txtTitleInfor.setTypeface(MainAppActivity.typeFaceFont);
		
		txtContact=(TextView) root.findViewById(R.id.txt_contact);
		txtContact.setTypeface(MainAppActivity.typeFaceFont);
		
		txtAddress=(TextView) root.findViewById(R.id.txt_ld_address);
		txtAddress.setTypeface(MainAppActivity.typeFaceFont);
		
		txtName=(TextView) root.findViewById(R.id.txt_ld_name);
		txtName.setTypeface(MainAppActivity.typeFaceFont);
		
		txtWebsite=(TextView) root.findViewById(R.id.txt_ld_website);
		txtWebsite.setTypeface(MainAppActivity.typeFaceFont);
		
		txtPhone=(TextView) root.findViewById(R.id.txt_ld_phone);
		txtPhone.setTypeface(MainAppActivity.typeFaceFont);
		
		txtEmail=(TextView) root.findViewById(R.id.txt_ld_email);
		txtEmail.setTypeface(MainAppActivity.typeFaceFont);
		
		txtInfor=(TextView) root.findViewById(R.id.txt_ld_infor);
		txtInfor.setTypeface(MainAppActivity.typeFaceFont);
		
		txtDistance=(TextView) root.findViewById(R.id.txt_ld_distance);
		txtDistance.setTypeface(MainAppActivity.typeFaceFont);
		
		btnMarkBack=(ImageButton) root.findViewById(R.id.btn_markback);
		btnCall=(ImageButton) root.findViewById(R.id.btn_call);
		btnBookmark=(ImageButton) root.findViewById(R.id.btn_bookmark);
		btnGetDirection=(ImageButton) root.findViewById(R.id.btn_direction);
		imageView=(ImageView) root.findViewById(R.id.ld_image);
		//get resource
		BOOKMARK_SUCCESS=getResources().getString(R.string.bookmark_success);
		//set data
		Date dateAdd=new Date();
		DateFormat dateFormat=new DateFormat();
		locate.setDateAdded(DateFormat.format("dd/MM/yyyy", dateAdd).toString());
		MainAppActivity.fileTranfer.setFileName(FileBookmarkRecent.RECENT_FILE);
		if(MainAppActivity.fileTranfer.loadAll().size()==ApplicationConstant.MAX_RECENT_ITEM)
			MainAppActivity.fileTranfer.deleteFirstItem();
		MainAppActivity.fileTranfer.add(locate);
		
		//set image
		//set image
				if(locate.getImages().size()>0)
				{
					imageView.setImageBitmap(locate.getImages().get(0));
				}
				else{
					locate.loadItemImage(handler2);
					Log.d("chua co item img", "---------");
				}
					
				txtAddress.setText(locate.getAdress());
				txtName.setText(locate.getName());
				txtWebsite.setText(locate.getWebsite());
				txtPhone.setText(locate.getPhone());
				txtEmail.setText(locate.getEmail());
				txtInfor.setText(locate.getInfor());
				txtDistance.setText(locate.getDistance()+" "+getResources().getString(R.string.unit));
				txtInfor.setText(locate.getInfor());
				txtInfor.setMovementMethod(new ScrollingMovementMethod());
				
				//action
				btnMarkBack.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						Intent intent=new Intent(getActivity(),MapActivity.class);
						intent.putExtra(ApplicationConstant.ACTION, ApplicationConstant.MARK_ON_MAP_ONE);
						startActivity(intent);
					}
				});
				if(locate.getPhone().length()==0){
					btnCall.setVisibility(View.GONE);
				}
				else
				btnCall.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
							Intent intent=new Intent(Intent.ACTION_DIAL,Uri.parse("tel:"+locate.getPhone()));
							startActivity(intent);
						
					}
				});
				imageView.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						Log.d("image click", "------------size"+locate.getImages().size());
						if(locate.getImageNames().size()>0){
							if(locate.getImages().size()==0||(locate.getImageNames().size()>locate.getImages().size())){
								Log.d("image click2", "------------size"+locate.getImages().size());
								locate.LoadAllImages(handler, progressDialog);
							}
							else{
								Log.d("image click1", "------------size"+locate.getImages().size());
								Intent intent=new Intent(getActivity(),PopupImages.class);
								startActivity(intent);
							}	
						}
					}
				});
				btnBookmark.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						DateFormat dateFormat=new DateFormat();
						Date dateAdd=new Date();
						locate.setDateAdded(DateFormat.format("dd/MM/yyyy", dateAdd).toString());
						MainAppActivity.fileTranfer.setFileName(FileBookmarkRecent.BOOKMARK_FILE);
						MainAppActivity.fileTranfer.add(locate);
						Toast.makeText(getActivity(), BOOKMARK_SUCCESS, Toast.LENGTH_SHORT).show();
					}
				});
				btnGetDirection.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						Intent intent=new Intent(getActivity(),GetDirection.class);
						GetDirection.MY_LOCATE_MARK=false;
						startActivity(intent);
					}
				});
		return root;
	}

	@Override
	public void navigateToLocateDetail(int postion) {
		// TODO Auto-generated method stub
		if(ApplicationConstant.IS_CALLING_BOOKMARK==true||ApplicationConstant.IS_CALLING_RECENT==true)
			locate=BookmarkRecentFragment.listLocate.get(postion);
		else
			locate=LoadLocate.listLocate.get(postion);	
		
	}
	Handler handler2=new Handler(){
		@Override
		public void handleMessage(Message msg) {
			Log.d("reviced message", "--------------");
			if(locate.getImages().size()>0){
				Log.d("khac null", "");
				imageView.setImageBitmap(locate.getImages().get(0));
			}
		};
	};
	Handler handler=new Handler(){
		@Override
		public void handleMessage(Message msg) {
			Log.d("da load xong","--------");
			Intent intent=new Intent(getActivity(),PopupImages.class);
			startActivity(intent);
			imageView.setImageBitmap(locate.getImages().get(0));
		};
	};
}
